<?php
	class HiringModel extends Model{
		public function hiring_show($hid){
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1 AND '.C('DB_PREFIX').'hiring.hiring_id = '.$hid)
						->find();
			$hirings['intro'] = str_replace("\n", "<br/>", $hirings['intro']);
			$sname = D('store')->getField('sid,store_name');
			$ch['check'] = $hirings['check']+1;
			D('hiring')->where('hiring_id='.$hid)->save($ch);
				$age = explode(',',$hirings['age']);
				$hirings['age_start'] = $age[0];
				$hirings['age_end'] = $age[1];
				$m['sid'] = array('in',$hirings['workplace']);
			$store = D('store')->where($m)->select();
			$hirings['store'] = $store;
			$data['cid'] = $hirings['cid'];
			$data['is_del'] = 0;
			$hirings['com_info'] = D('company_info')->where($data)->find();
			$list = D('company_pic')->where($data)->order('ctime desc')->select();
			foreach($list as $k => &$v){
				$v['pic'] = get_img_url($v['fid']);
			}
			$hirings['com_pic'] = $list;
			return $hirings;
		}

		public function getlist($jianzhi=NULL){
			if($jianzhi == 7){
				$result = C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1 AND '.C('DB_PREFIX').'hiring_info.worktime = 7';
			}else{
				$result = C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1 AND '.C('DB_PREFIX').'hiring_info.worktime != 7';
			}
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where($result)
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->findPage(20);
			$sname = D('store')->getField('sid,store_name');
			foreach($hirings['data'] as $k => &$v){
				$v['title'] = mb_substr($v['title'],0,17,'utf-8');
				$age = explode(',',$v['age']);
				$v['age_start'] = $age[0];
				$v['age_end'] = $age[1];
				$store = explode(',',$v['workplace']);
				foreach ($store as $key => $value) {
					$store_name .= $sname[$store[$key]].',';
				}
				$v['sname'] =  mb_substr($store_name,0,50,'utf-8');
			}
			return $hirings;
		}

		public function getsearchlist($jianzhi=NULL,$area,$map){
			$store = D('store')->where($area)->getField('sid',true);
				$select = D('hiring')->where($map)->select();
			
				foreach ($select as $k => &$v) {
					$s = explode(',', $v['workplace']);
					foreach ($store as $key => &$value) {
						if (in_array($value,$s)){
							$hid .=$v['hiring_id'].",";
						}
					}
				}
			$arr = explode(',',$hid);
			$hiringid = array_unique($arr);
			$hiringid = array_filter($hiringid);
			$hiringid = implode(',',$hiringid);
			if($jianzhi == 7){
				$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1 AND '.C('DB_PREFIX').'hiring_info.worktime = 7 AND '.C('DB_PREFIX').'hiring.hiring_id IN('.$hiringid.')')
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->findPage(20);
			}else{
				$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1 AND '.C('DB_PREFIX').'hiring_info.worktime != 7 AND '.C('DB_PREFIX').'hiring.hiring_id IN('.$hiringid.')')
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->findPage(20);
			}
			
			$sname = D('store')->getField('sid,store_name');
			foreach($hirings['data'] as $k => &$v){
				$v['title'] = mb_substr($v['title'],0,17,'utf-8');
				$age = explode(',',$v['age']);
				$v['age_start'] = $age[0];
				$v['age_end'] = $age[1];
				$store = explode(',',$v['workplace']);
				foreach ($store as $key => $value) {
					$store_name .= $sname[$store[$key]].',';
				}
				$v['sname'] = mb_substr($store_name,0,50,'utf-8');
			}
			return $hirings;
		}

		public function getlistbymax($max,$map_x,$map_y){
			$storeinmax = D('store')->where("(map_x BETWEEN ".$max[1]." AND ".$max[0].") AND (map_y BETWEEN ".$max[3]." AND ".$max[2].") AND is_del=0")->getField('sid',true);
			$result = C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.status = 1';
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where($result)
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->select();
			$sname = D('store')->getField('sid,store_name');
			foreach($hirings as $k => &$v){
				$store = explode(',',$v['workplace']);
				$hiringinmax = array_intersect($store,$storeinmax);
				if(!empty($hiringinmax)){
					$age = explode(',',$v['age']);
					$v['age_start'] = $age[0];
					$v['age_end'] = $age[1];
					$store_name = '';
					foreach ($store as $key => $value) {
						$store_name .= $sname[$store[$key]].',';
					}
					$v['sname'] = mb_substr($store_name,0,50,'utf-8');
					$temp = array_rand($hiringinmax,1);
					$location = D('store')->where('sid = '.$hiringinmax[$temp])->find();
					$v['far'] = GetDistance($location['map_x'],$location['map_y'],$map_x,$map_y);
					$hiringsinmax[] = $v;
				}
			}
			return $hiringsinmax;
		}

		public function getlisttop(){
			$t = strtotime("-1 week");
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.ontop = 1 AND '.C('DB_PREFIX').'hiring.ontop_time >= '.$t.' AND '.C('DB_PREFIX').'hiring.status = 1')
						->order(C('DB_PREFIX').'hiring.ontop_time DESC')
						->select();
			$hirings_all = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.ontop = 1  AND '.C('DB_PREFIX').'hiring.status = 1')
						->select();
			foreach($hirings_all as $k => &$v){
				$t = strtotime("-1 week");
				if($v['ontop_time']<$t){
					D('hiring')->where('hiring_id = '.$v['hiring_id'])->save(array('ontop'=>0));
				}
			}
			$sname = D('store')->getField('sid,store_name');
			foreach($hirings as $k => &$v){
				$v['title'] = mb_substr($v['title'],0,17,'utf-8');
				$age = explode(',',$v['age']);
				$v['age_start'] = $age[0];
				$v['age_end'] = $age[1];
				$store = explode(',',$v['workplace']);
				foreach ($store as $key => $value) {
					$store_name .= $sname[$store[$key]].',';
				}
				$v['sname'] = mb_substr($store_name,0,50,'utf-8');
			}
			return $hirings;
		}
}
?>